package com.lz.ovuola.util.fastdfs;

import java.io.IOException;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lz.ovuola.util.exception.BusinessException;
import com.lz.ovuola.util.exception.msg.SysSecurityMsgEnum;
import com.lz.ovuola.util.threadlocal.UserContextHolder;

/**
 * 上传下载
 * 
 * @author fz
 *
 */
public abstract class FileUtils {

	private static Logger logger = LoggerFactory.getLogger("FileUtils");

	/**
	 * 
	 * @param filePath
	 * @param fileType
	 * @return
	 */
	public static final String uploadFile(String filePath, String fileType) {
		String fileid = "";
		try {
			NameValuePair[] meta_list = null;
			fileid = get().upload_file1(filePath, fileType, meta_list);
			logger.info("userId:{} Upload local file  ok, fileid={}",
					UserContextHolder.get().getUserId(), fileid);
		} catch (IOException e) {
			logger.error("userId:{} Upload local file  fail", UserContextHolder
					.get().getUserId());
			throw new BusinessException(
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getStatus(),
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getMsg());
		} catch (MyException e) {
			logger.error("userId:{} Upload local file  fail", UserContextHolder
					.get().getUserId());
			throw new BusinessException(
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getStatus(),
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getMsg());
		}
		return DfsConfig.getDownload() + fileid;
	}

	/**
	 * 
	 * @param fileBuff
	 * @param fileType
	 * @return
	 */
	public static final String uploadFile(byte[] fileBuff, String fileType) {
		String fileid = "";
		try {
			NameValuePair[] meta_list = null;
			fileid = get().upload_file1(fileBuff, fileType, meta_list);
			logger.info("userId:{} Upload local file  ok, fileid={}",
					UserContextHolder.get().getUserId(), fileid);
		} catch (IOException e) {
			logger.error("userId:{} Upload local file  fail", UserContextHolder
					.get().getUserId());
			throw new BusinessException(
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getStatus(),
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getMsg());
		} catch (MyException e) {
			logger.error("userId:{} Upload local file  fail", UserContextHolder
					.get().getUserId());
			throw new BusinessException(
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getStatus(),
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getMsg());
		}
		return DfsConfig.getDownload() + fileid;
	}

	/**
	 * 
	 * @return
	 * @throws IOException
	 */
	private static StorageClient1 get() throws IOException {
		StorageServer ss = DfsConfig.getTc().getStoreStorage(DfsConfig.getTs());
		if (ss == null) {
			logger.error("StorageServer is null");
			throw new BusinessException(
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getStatus(),
					SysSecurityMsgEnum.UPLOAD_FILE_IS_FAIL.getMsg());
		}
		StorageClient1 sc1 = new StorageClient1(DfsConfig.getTs(), ss);
		return sc1;
	}
}
